{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 导入数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "from sklearn.datasets import load_iris\n",
    "from sklearn.model_selection import train_test_split, GridSearchCV, cross_val_score\n",
    "from sklearn.linear_model import LogisticRegression as LR\n",
    "from sklearn.preprocessing import StandardScaler\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "data=load_iris()\n",
    "x=data['data']\n",
    "y=data['target']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>sepal length (cm)</th>\n",
       "      <th>sepal width (cm)</th>\n",
       "      <th>petal length (cm)</th>\n",
       "      <th>petal width (cm)</th>\n",
       "      <th>label</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>5.1</td>\n",
       "      <td>3.5</td>\n",
       "      <td>1.4</td>\n",
       "      <td>0.2</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>4.9</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.4</td>\n",
       "      <td>0.2</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>4.7</td>\n",
       "      <td>3.2</td>\n",
       "      <td>1.3</td>\n",
       "      <td>0.2</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4.6</td>\n",
       "      <td>3.1</td>\n",
       "      <td>1.5</td>\n",
       "      <td>0.2</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5.0</td>\n",
       "      <td>3.6</td>\n",
       "      <td>1.4</td>\n",
       "      <td>0.2</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>145</th>\n",
       "      <td>6.7</td>\n",
       "      <td>3.0</td>\n",
       "      <td>5.2</td>\n",
       "      <td>2.3</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>146</th>\n",
       "      <td>6.3</td>\n",
       "      <td>2.5</td>\n",
       "      <td>5.0</td>\n",
       "      <td>1.9</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>147</th>\n",
       "      <td>6.5</td>\n",
       "      <td>3.0</td>\n",
       "      <td>5.2</td>\n",
       "      <td>2.0</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>148</th>\n",
       "      <td>6.2</td>\n",
       "      <td>3.4</td>\n",
       "      <td>5.4</td>\n",
       "      <td>2.3</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>149</th>\n",
       "      <td>5.9</td>\n",
       "      <td>3.0</td>\n",
       "      <td>5.1</td>\n",
       "      <td>1.8</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>150 rows × 5 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     sepal length (cm)  sepal width (cm)  petal length (cm)  petal width (cm)  \\\n",
       "0                  5.1               3.5                1.4               0.2   \n",
       "1                  4.9               3.0                1.4               0.2   \n",
       "2                  4.7               3.2                1.3               0.2   \n",
       "3                  4.6               3.1                1.5               0.2   \n",
       "4                  5.0               3.6                1.4               0.2   \n",
       "..                 ...               ...                ...               ...   \n",
       "145                6.7               3.0                5.2               2.3   \n",
       "146                6.3               2.5                5.0               1.9   \n",
       "147                6.5               3.0                5.2               2.0   \n",
       "148                6.2               3.4                5.4               2.3   \n",
       "149                5.9               3.0                5.1               1.8   \n",
       "\n",
       "     label  \n",
       "0        0  \n",
       "1        0  \n",
       "2        0  \n",
       "3        0  \n",
       "4        0  \n",
       "..     ...  \n",
       "145      2  \n",
       "146      2  \n",
       "147      2  \n",
       "148      2  \n",
       "149      2  \n",
       "\n",
       "[150 rows x 5 columns]"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data=pd.DataFrame(x,columns=data.feature_names)\n",
    "data['label']=y\n",
    "data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 切分数据集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(45,)"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xtrain,xtest,ytrain,ytest=train_test_split(x,y,test_size=0.3,random_state=420)\n",
    "ytest.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 使用标准化包，对训练集来学习，从而对训练集和测试集来做标准化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [],
   "source": [
    "std=StandardScaler()\n",
    "xtrain_=std.fit_transform(xtrain)\n",
    "xtest_=std.fit_transform(xtest)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-1.53220047e-01, -1.37279645e-01,  5.45585397e-01,\n",
       "         5.19486735e-01],\n",
       "       [ 1.08786233e+00, -1.37279645e-01,  1.33560386e+00,\n",
       "         1.48206510e+00],\n",
       "       [ 2.60474080e+00, -1.37279645e-01,  1.82176906e+00,\n",
       "         1.34455390e+00],\n",
       "       [-1.39430243e+00,  3.77519023e-01, -1.39907542e+00,\n",
       "        -1.26815879e+00],\n",
       "       [-9.80608300e-01,  8.92317690e-01, -1.27753412e+00,\n",
       "        -1.26815879e+00],\n",
       "       [ 1.22576037e-01, -9.09477646e-01,  3.02502794e-01,\n",
       "        -1.68069238e-01],\n",
       "       [-7.04812216e-01,  1.14971702e+00, -1.27753412e+00,\n",
       "        -1.26815879e+00],\n",
       "       [-2.91118089e-01, -1.16687698e+00,  4.84814746e-01,\n",
       "         1.06953151e-01],\n",
       "       [ 3.98372122e-01, -1.37279645e-01,  7.27897349e-01,\n",
       "         9.32020318e-01],\n",
       "       [ 5.36270164e-01, -1.37279645e-01,  6.06356048e-01,\n",
       "         3.81975540e-01],\n",
       "       [ 1.63945450e+00,  1.20119689e-01,  1.09252125e+00,\n",
       "         1.34455390e+00],\n",
       "       [-4.29016131e-01, -1.37279645e-01,  5.45585397e-01,\n",
       "         5.19486735e-01],\n",
       "       [-1.53220047e+00,  1.40711636e+00, -1.58138738e+00,\n",
       "        -1.26815879e+00],\n",
       "       [-9.80608300e-01, -1.37279645e-01, -1.21676347e+00,\n",
       "        -1.26815879e+00],\n",
       "       [ 1.50155646e+00, -1.37279645e-01,  1.15329190e+00,\n",
       "         1.34455390e+00],\n",
       "       [ 1.22576037e-01, -9.09477646e-01,  1.80961493e-01,\n",
       "         1.06953151e-01],\n",
       "       [-1.80799655e+00,  3.77519023e-01, -1.39907542e+00,\n",
       "        -1.26815879e+00],\n",
       "       [-1.53220047e+00,  8.92317690e-01, -1.33830477e+00,\n",
       "        -1.13064760e+00],\n",
       "       [-1.25640438e+00,  8.92317690e-01, -1.21676347e+00,\n",
       "        -1.26815879e+00],\n",
       "       [-8.42710258e-01,  1.92191502e+00, -1.21676347e+00,\n",
       "        -1.26815879e+00],\n",
       "       [-4.29016131e-01,  8.92317690e-01, -1.15599282e+00,\n",
       "        -1.26815879e+00],\n",
       "       [ 1.36365842e+00, -1.37279645e-01,  9.70979951e-01,\n",
       "         1.61957629e+00],\n",
       "       [-2.91118089e-01, -1.42427631e+00,  2.41732144e-01,\n",
       "         2.44464346e-01],\n",
       "       [-1.25640438e+00, -1.37279645e-01, -1.33830477e+00,\n",
       "        -1.40566999e+00],\n",
       "       [ 1.08786233e+00, -6.52078312e-01,  6.06356048e-01,\n",
       "         5.19486735e-01],\n",
       "       [ 8.12066248e-01,  6.34918356e-01,  6.67126698e-01,\n",
       "         6.56997929e-01],\n",
       "       [-8.42710258e-01,  8.92317690e-01, -1.27753412e+00,\n",
       "        -1.26815879e+00],\n",
       "       [-2.91118089e-01,  2.95151236e+00, -1.33830477e+00,\n",
       "        -1.26815879e+00],\n",
       "       [-2.91118089e-01, -1.68167565e+00,  5.94201917e-02,\n",
       "        -1.68069238e-01],\n",
       "       [ 1.22576037e+00, -3.94678978e-01,  6.06356048e-01,\n",
       "         2.44464346e-01],\n",
       "       [-9.80608300e-01, -1.93907498e+00, -1.83662411e-01,\n",
       "        -1.68069238e-01],\n",
       "       [ 8.12066248e-01, -9.09477646e-01,  7.88667999e-01,\n",
       "         9.32020318e-01],\n",
       "       [ 1.36365842e+00,  6.34918356e-01,  1.27483320e+00,\n",
       "         1.34455390e+00],\n",
       "       [-9.80608300e-01,  6.34918356e-01, -1.33830477e+00,\n",
       "        -1.26815879e+00],\n",
       "       [-8.42710258e-01,  1.14971702e+00, -1.33830477e+00,\n",
       "        -1.13064760e+00],\n",
       "       [ 9.49964291e-01,  3.77519023e-01,  5.45585397e-01,\n",
       "         5.19486735e-01],\n",
       "       [ 8.12066248e-01,  8.92317690e-01,  1.21406255e+00,\n",
       "         1.75708748e+00],\n",
       "       [-1.53220047e-01, -3.94678978e-01, -1.35045890e-03,\n",
       "         2.44464346e-01],\n",
       "       [ 2.60474080e-01,  3.77519023e-01,  7.27897349e-01,\n",
       "         9.32020318e-01],\n",
       "       [-1.53220047e-02, -6.52078312e-01,  5.45585397e-01,\n",
       "         2.44464346e-01],\n",
       "       [-2.91118089e-01, -1.68167565e+00,  1.20190842e-01,\n",
       "        -3.05580432e-02],\n",
       "       [ 6.74168206e-01, -3.94678978e-01,  4.24044096e-01,\n",
       "         2.44464346e-01],\n",
       "       [-2.91118089e-01, -1.93907498e+00,  2.41732144e-01,\n",
       "         2.44464346e-01],\n",
       "       [ 3.98372122e-01, -9.09477646e-01,  9.10209301e-01,\n",
       "         6.56997929e-01],\n",
       "       [ 1.08786233e+00, -1.37279645e-01,  1.15329190e+00,\n",
       "         9.32020318e-01]])"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xtest_"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 在确定l2范式的情况下，使用网格搜索判断solver, C的最优组合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "GridSearchCV(cv=5, estimator=LogisticRegression(max_iter=10000),\n",
       "             param_grid={'C': [0.05, 0.10277777777777777, 0.15555555555555556,\n",
       "                               0.20833333333333331, 0.2611111111111111,\n",
       "                               0.3138888888888889, 0.36666666666666664,\n",
       "                               0.41944444444444445, 0.4722222222222222, 0.525,\n",
       "                               0.5777777777777778, 0.6305555555555556,\n",
       "                               0.6833333333333333, 0.7361111111111112,\n",
       "                               0.788888888888889, 0.8416666666666667,\n",
       "                               0.8944444444444445, 0.9472222222222223, 1.0],\n",
       "                         'solver': ['liblinear', 'sag', 'newton-cg', 'lbfgs']})"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p={\n",
    "    'C':list(np.linspace(0.05,1,19)),\n",
    "    'solver':['liblinear','sag','newton-cg','lbfgs']\n",
    "}\n",
    "model=LR(penalty='l2',max_iter=10000)\n",
    "GS=GridSearchCV(model,p,cv=5)\n",
    "GS.fit(xtrain_,ytrain)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'C': 0.41944444444444445, 'solver': 'sag'}"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "GS.best_params_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.9714285714285715"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "GS.best_score_"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 将最优的结果重新用来实例化模型，查看训练集和测试集下的分数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 将最优参数重新用于实例化模型，查看训练集和测试集下的分数\n",
    "model=LR(penalty='l2',max_iter=10000,\n",
    "        C=GS.best_params_['C'],\n",
    "        solver=GS.best_params_['solver'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "LogisticRegression(C=0.41944444444444445, max_iter=10000, solver='sag')"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.fit(xtrain_,ytrain)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.9714285714285714"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.score(xtrain_,ytrain)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.8444444444444444"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.score(xtest_,ytest)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 计算精准率"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn import metrics"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[15,  0,  0],\n",
       "       [ 0, 13,  7],\n",
       "       [ 0,  0, 10]])"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "metrics.confusion_matrix(ytest,model.predict(xtest_))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.8444444444444444"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#精准率\n",
    "metrics.precision_score(ytest,model.predict(xtest_),average='micro')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.4"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
